package edu.infomatik.tundra.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

import edu.infomatik.tundra.data.DAOtundra;
import edu.infomatik.tundra.data.IBean;
import edu.infomatik.tundra.logic.SprintBean;
import edu.infomatik.tundra.logic.SprintMetrics;
import edu.infomatik.tundra.logic.UStoryBean;
import edu.infomatik.tundra.logic.WorkBean;
import edu.infomatik.tundra.logic.WorkKinds;
import edu.infomatik.tundra.logic.security.SystemAccessBean;

@SuppressWarnings("unused")
public class TheProject {
	@SessionState
	@Property
	private WorkBean project;
	private boolean projectExists;
	@SessionState
	private SprintBean sessionSprint;
	@SessionState
	private UStoryBean sessionStory;	
	@SessionState
	private SystemAccessBean user;
	private boolean userExists;
	@SessionState
	private Boolean newWork=new Boolean(false);
	@Property
	private UStoryBean story;
	@Property
	private SprintBean sprint;
	private List<UStoryBean> storyList;
	private List<SprintBean> sprintList;
	
	@Inject
	private Messages message;
	
	public String getMetrics(){
		return message.get("SprintMetrics."+SprintMetrics.get(sprint.getMetrics()).getName());
	}
	
	public String getLinkedWithName() {
		String result="";
		if (! storyList.isEmpty()){
			for (UStoryBean usb : storyList)
				if (story.getLinkedWith()==usb.getId())
					result="<strong>"+usb.getName()+"</strong>";	
		}
		return result;
	}
	public String getSprintIdName() {
		String result="";
		if (! sprintList.isEmpty()){
			for (SprintBean sb : sprintList)
				if (story.getSprintId()==sb.getId())
					result="<strong>"+sb.getName()+"</strong>";	
		}
		return result;
	}

	public String getProjectName() {
		if (projectExists) return project.getName();
		return "";
	}
	String onActivate(){
		if (!userExists) return "Index";
		else if (!projectExists) return "ProjectList"; 
		getAllStories();
		getAllSprints();
		return null;
	}
	
	public List<UStoryBean> getAllStories(){
		storyList=new ArrayList<UStoryBean>();
		for (IBean usb: DAOtundra.getInstance().getAllBeans(new UStoryBean())){
			if (((UStoryBean)usb).getKind()==WorkKinds.USTORY_VALUE && ((UStoryBean)usb).getProjectId()==project.getId())
				storyList.add((UStoryBean)usb);
		}
		return storyList;
	}
	
	public List<SprintBean> getAllSprints(){
	sprintList=new ArrayList<SprintBean>();
		for (IBean sb: DAOtundra.getInstance().getAllBeans(new SprintBean())){
			if (((SprintBean)sb).getKind()==WorkKinds.SPRINT_VALUE)
				if (((SprintBean)sb).getProjectId()==project.getId())
					sprintList.add((SprintBean)sb);
		}
		return sprintList;
	}

//	@OnEvent(component="storyNameClick")
	String onActionFromStoryNameClick(long id){
		for(UStoryBean usb: storyList)
			if (usb.getId()==id) sessionStory=usb;
		return "TheStory";
	}

	//	@OnEvent(component="deleteStory")
	String onActionFromDeleteStory(long id){
		if (!storyList.isEmpty()){
			for(UStoryBean bean: storyList){
				if (bean.getId()==id) {
					DAOtundra.getInstance().delete(bean);
					storyList=getAllStories();
					return "TheProject";
				}
			}
		}
		return null;
	}
	
	@OnEvent(component="editStory")
	String onEditStoryClicked(long id){
		if (!storyList.isEmpty())
			for(UStoryBean bean: storyList)
				if (bean.getId()==id) {
					sessionStory=bean;
					sessionSprint=null;
					return "CreateUStory";
				}else {
					sessionStory=null;
					sessionSprint=null;
				}
		return null;
	}

//	@OnEvent(component="sprintNameClick")
	String onActionFromSprintNameClick(long id){
		for(SprintBean sb: sprintList)
			if (sb.getId()==id) sessionSprint=sb;
		return "TheSprint";
//		return null;
	}

	
	String onActionFromDeleteSprint(long id){
		if (!sprintList.isEmpty()){
			for(SprintBean bean: sprintList){
				if (bean.getId()==id) {
					DAOtundra.getInstance().delete(bean);
					sprintList=getAllSprints();
					return "TheProject";
				}
			}
		}
		return null;
	}
	
	@OnEvent(component="editSprint")
	String onEditSprintClicked(long id){
		if (!sprintList.isEmpty())
			for(SprintBean bean: sprintList)
				if (bean.getId()==id) {
					sessionStory=null;
					sessionSprint=bean;
					return "CreateSprint";
				}else {
					sessionStory=null;
					sessionSprint=null;
				}
		return null;
	}
}
